home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / GRAPHICS / GIF2RPC.SPK / source / 16bpp_66bit / c / sierra3 < prev    next >
Text File  |  1995-10-16  |  5KB  |  124 lines

  1. /* sierra3.c
  2.  * AUTHOR:      Cy Booker, cy@cheepnis.demon.co.uk
  3.  * LICENSE:     FreeWare, Copyright (c) 1995 Cy Booker
  4.  *
  5.  * filter:              *  5  3
  6.  *                2  4  5  4  2
  7.  *                   2  3  2            (1/32)
  8.  */
  9.  
  10. #include "internal.h"
  11.  
  12. #include <assert.h>
  13. #include <string.h>             /* memset() */
  14. #include <stdlib.h>             /* calloc() */
  15.  
  16. #include "OS:hourglass.h"
  17. #include "OS:macros.h"
  18.  
  19.  
  20.  
  21. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  22.  */
  23.  
  24. extern bool process_gif_16bpp_sierra3_66bit(
  25.                 const process_gif       *p) {
  26.   byte                  *rove;
  27.   int                   width, height;
  28.   int                   x, y;
  29.   const os_colour       *palette;
  30.   int                   line_length;
  31.   int                   *buffer, *this_row, *next_row, *botm_row;
  32.   int                   buffer_width;
  33.   int                   r, g, b;
  34.   int                   or, og, ob;
  35.   int                   t;
  36.   int                   er, eg, eb;
  37.   int                   rr, rg, rb;     /* remainders */
  38.  
  39.   assert(p);
  40.   assert(p->pixel_width > 0);
  41.   assert(p->pixel_height > 0);
  42.   assert(p->in_palette.colours);
  43.  
  44.   /*
  45.    * sierra3 requires storing error info for two pixels to right and two pixels to left
  46.    * so we will just allocate two extra columns for each row and not do any edge checks
  47.    * each entry is stored as {r*SCALE, g*SCALE, b*SCALE}
  48.    */
  49.   buffer_width = (2 + p->pixel_width + 2) * 3;
  50.   buffer = calloc(1, sizeof(*buffer) * (buffer_width * 3));
  51.   if (!buffer) {
  52.     /*
  53.      * oh dear
  54.      */
  55.     return TRUE;
  56.   }
  57.  
  58.   initialise_scaling_tables();
  59.  
  60.   /*
  61.    * not we pre-load values from the process_gif array
  62.    * because it considerably helps the compiler produce better code
  63.    */
  64.   rove = p->buffer;
  65.   width = p->pixel_width;
  66.   height = p->pixel_height;
  67.   palette = p->in_palette.colours;
  68.   line_length = p->line_length;
  69.   for (y= height; (y > 0); y--) {
  70.     if ((y & 7) == 0) {
  71.       xhourglass_percentage((y * 100) / height);
  72.     }
  73.     this_row = buffer + (buffer_width * ((y + 3) % 3)) + 2*3;
  74.     next_row = buffer + (buffer_width * ((y + 2) % 3)) + 2*3;
  75.     botm_row = buffer + (buffer_width * ((y + 1) % 3)) + 2*3;
  76.     /* bottom row has no errors */
  77.     memset(botm_row, 0, sizeof(*botm_row) * (buffer_width - 2*3));
  78.     /*
  79.      * note that just because we are actually scanning/outputting right to left
  80.      * doesn't matter as far as the filter is concerned
  81.      * although it might help if we could ``snake''
  82.      */
  83.     for (x= width - 1; (x >= 0); x--) {
  84.       INPUT;
  85.       r += *this_row++;                                 /* add in errors from all rows */
  86.       g += *this_row++;
  87.       b += *this_row++;
  88.       PROCESS;
  89.       r -= or;  g -= og; b-= ob;                        /* error */
  90.       er = (r * 5) / 32; eg = (g * 5) / 32; eb = (b * 5) / 32;
  91.       this_row[ 0] += er; this_row[ 1] += eg; this_row[ 2] += eb;       /* this[ 1] += 5/32 */
  92.       next_row[ 0] += er; next_row[ 1] += eg; next_row[ 2] += eb;       /* next[ 0] += 5/32 */
  93.       rr = r - (2*er); rg = g - (2*eg); rb = b - (2*eb);                /* remainder of error left */
  94.  
  95.       er = (r * 4) / 32; eg = (g * 4) / 32; eb = (b * 4) / 32;
  96.       next_row[ 3] += er; next_row[ 4] += eg; next_row[ 5] += eb;       /* next[ 1] += 4/32 */
  97.       next_row[-3] += er; next_row[-2] += eg; next_row[-1] += eb;       /* next[-1] += 4/32 */
  98.       rr -= 2*er; rg -= 2*eg; rb -= 2*eb;                               /* remainder of error left */
  99.  
  100.       er = (r * 3) / 32; eg = (g * 3) / 32; eb = (b * 3) / 32;
  101.       this_row[ 3] += er; this_row[ 4] += eg; this_row[ 5] += eb;       /* this[ 2] += 3/32 */
  102.       botm_row[ 0] += er; botm_row[ 1] += eg; botm_row[ 2] += eb;       /* botm[ 0] += 3/32 */
  103.       rr -= 2*er; rg -= 2*eg; rb -= 2*eb;                               /* remainder of error left */
  104.  
  105.       er = (r * 2) / 32; eg = (g * 2) / 32; eb = (b * 2) / 32;
  106.       next_row[ 6] += er; next_row[ 7] += eg; next_row[ 8] += eb;       /* next[ 2] += 2/32 */
  107.       next_row[-6] += er; next_row[-5] += eg; next_row[-4] += eb;       /* next[-2] += 2/32 */
  108.       botm_row[-3] += er; botm_row[-2] += eg; botm_row[-1] += eb;       /* botm[-1] += 2/32 */
  109.       rr -= 3*er; rg -= 3*eg; rb -= 3*eb;                               /* remainder of error left */
  110.  
  111.       botm_row += 3;                                    /* adjust next_row pointer */
  112.       botm_row[ 0] += rr; botm_row[ 1] += rg; botm_row[ 2] += rb;       /* botm[ 1] += 2/32 */
  113.  
  114.       next_row += 3;                                    /* adjust next_row pointer */
  115.     }
  116.     rove += line_length;
  117.   }
  118.   free(buffer);
  119.   return FALSE;
  120. }
  121.  
  122.  
  123.  
  124.